/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ButtonEditor.java
 *
 * Created on 13.Haz.2009, 13:14:38
 */

package com.meraklisina.latexeditor.wizard ;

import javax.swing.JDialog ;

import com.meraklisina.latexeditor.LatexButton ;

/**
 *
 * @author kursattopcu
 */
public class ButtonEditor extends javax.swing.JPanel
{
    private JDialog dialog ;

    /** Creates new form ButtonEditor */
    public ButtonEditor( JDialog jdialog )
    {
        this.dialog = jdialog ;
        this.initComponents( ) ;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings( "unchecked" )
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents( )
    {

        this.BtnCancel = new javax.swing.JButton( ) ;
        this.nameTxt = new javax.swing.JTextField( ) ;
        this.jLabel1 = new javax.swing.JLabel( ) ;
        this.jLabel2 = new javax.swing.JLabel( ) ;
        this.tooltipTxt = new javax.swing.JTextField( ) ;
        this.jLabel3 = new javax.swing.JLabel( ) ;
        this.prefixTxt = new javax.swing.JTextField( ) ;
        this.jLabel4 = new javax.swing.JLabel( ) ;
        this.suffixTxt = new javax.swing.JTextField( ) ;
        this.jLabel5 = new javax.swing.JLabel( ) ;
        this.jScrollPane1 = new javax.swing.JScrollPane( ) ;
        this.exampleCommandTxt = new javax.swing.JTextArea( ) ;
        this.header2 = new javax.swing.JPanel( ) ;
        this.label3 = new javax.swing.JLabel( ) ;
        this.label4 = new javax.swing.JLabel( ) ;
        this.addButton = new javax.swing.JButton( ) ;
        this.jLabel6 = new javax.swing.JLabel( ) ;
        this.imagePathTxt = new javax.swing.JTextField( ) ;
        this.selectImageBtn = new javax.swing.JButton( ) ;
        this.errLbl = new javax.swing.JLabel( ) ;

        this.BtnCancel.setText( "Cancel" ) ;
        this.BtnCancel.setToolTipText( "Cancel" ) ;
        this.BtnCancel.setName( "BtnCancel" ) ; // NOI18N
        this.BtnCancel.addActionListener( new java.awt.event.ActionListener( )
        {
            public void actionPerformed( java.awt.event.ActionEvent evt )
            {
                ButtonEditor.this.BtnCancelActionPerformed( evt ) ;
            }
        } ) ;

        this.nameTxt.setName( "nameTxt" ) ; // NOI18N

        this.jLabel1.setText( "Action Button Name:" ) ;
        this.jLabel1.setName( "jLabel1" ) ; // NOI18N

        this.jLabel2.setText( "Tool Tip:" ) ;
        this.jLabel2.setName( "jLabel2" ) ; // NOI18N

        this.tooltipTxt.setName( "tooltipTxt" ) ; // NOI18N

        this.jLabel3.setText( "Prefix:" ) ;
        this.jLabel3.setName( "jLabel3" ) ; // NOI18N

        this.prefixTxt.setName( "prefixTxt" ) ; // NOI18N

        this.jLabel4.setText( "Suffix:" ) ;
        this.jLabel4.setName( "jLabel4" ) ; // NOI18N

        this.suffixTxt.setName( "suffixTxt" ) ; // NOI18N

        this.jLabel5.setText( "Example Command:" ) ;
        this.jLabel5.setName( "jLabel5" ) ; // NOI18N

        this.jScrollPane1.setName( "jScrollPane1" ) ; // NOI18N

        this.exampleCommandTxt.setColumns( 20 ) ;
        this.exampleCommandTxt.setFont( new java.awt.Font( "Tahoma", 0, 11 ) ) ; // NOI18N
        this.exampleCommandTxt.setRows( 5 ) ;
        this.exampleCommandTxt.setName( "exampleCommandTxt" ) ; // NOI18N
        this.jScrollPane1.setViewportView( this.exampleCommandTxt ) ;

        this.header2.setBackground( new java.awt.Color( 153, 153, 255 ) ) ;
        this.header2.setName( "header2" ) ; // NOI18N

        this.label3.setFont( new java.awt.Font( "Tahoma", 1, 12 ) ) ;
        this.label3.setText( "Action Button Configuration" ) ;
        this.label3.setName( "label3" ) ; // NOI18N

        this.label4.setForeground( new java.awt.Color( 255, 255, 255 ) ) ;
        this.label4.setText( "Please provide necessary information for the button in toolbar." ) ;
        this.label4.setName( "label4" ) ; // NOI18N

        javax.swing.GroupLayout header2Layout = new javax.swing.GroupLayout( this.header2 ) ;
        this.header2.setLayout( header2Layout ) ;
        header2Layout.setHorizontalGroup( header2Layout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING ).addGroup( header2Layout.createSequentialGroup( ).addContainerGap( ).addGroup( header2Layout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING ).addComponent( this.label4, javax.swing.GroupLayout.DEFAULT_SIZE, 423, Short.MAX_VALUE ).addComponent( this.label3 ) ).addContainerGap( ) ) ) ;
        header2Layout.setVerticalGroup( header2Layout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING ).addGroup( header2Layout.createSequentialGroup( ).addContainerGap( ).addComponent( this.label3 ).addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED ).addComponent( this.label4 ).addContainerGap( javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE ) ) ) ;

        this.addButton.setText( "Add" ) ;
        this.addButton.setToolTipText( "Add" ) ;
        this.addButton.setName( "addButton" ) ; // NOI18N
        this.addButton.addActionListener( new java.awt.event.ActionListener( )
        {
            public void actionPerformed( java.awt.event.ActionEvent evt )
            {
                ButtonEditor.this.addButtonActionPerformed( evt ) ;
            }
        } ) ;

        this.jLabel6.setText( "Image:" ) ;
        this.jLabel6.setName( "jLabel6" ) ; // NOI18N

        this.imagePathTxt.setEnabled( false ) ;
        this.imagePathTxt.setName( "imagePathTxt" ) ; // NOI18N

        this.selectImageBtn.setText( "..." ) ;
        this.selectImageBtn.setToolTipText( "Select Image" ) ;
        this.selectImageBtn.setName( "selectImageBtn" ) ; // NOI18N
        this.selectImageBtn.addActionListener( new java.awt.event.ActionListener( )
        {
            public void actionPerformed( java.awt.event.ActionEvent evt )
            {
                ButtonEditor.this.selectImageBtnActionPerformed( evt ) ;
            }
        } ) ;

        this.errLbl.setForeground( java.awt.Color.red ) ;
        this.errLbl.setName( "errLbl" ) ; // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout( this ) ;
        this.setLayout( layout ) ;
        layout.setHorizontalGroup( layout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING ).addComponent( this.header2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE ).addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup( ).addContainerGap( ).addGroup( layout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING ).addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup( ).addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED, 293, javax.swing.GroupLayout.PREFERRED_SIZE ).addComponent( this.addButton ).addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED ).addComponent( this.BtnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE ) ).addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup( ).addComponent( this.jLabel5 ).addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.UNRELATED ).addComponent( this.jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 319, Short.MAX_VALUE ) ).addGroup( layout.createSequentialGroup( ).addComponent( this.jLabel4 ).addGap( 71, 71, 71 ).addComponent( this.suffixTxt, javax.swing.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE ) ).addGroup( layout.createSequentialGroup( ).addComponent( this.jLabel3 ).addGap( 71, 71, 71 ).addComponent( this.prefixTxt, javax.swing.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE ) ).addGroup( layout.createSequentialGroup( ).addGroup( layout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING ).addComponent( this.jLabel1 ).addComponent( this.jLabel2 ).addComponent( this.jLabel6 ) ).addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED ).addGroup( layout.createParallelGroup( javax.swing.GroupLayout.Alignment.TRAILING ).addComponent( this.tooltipTxt, javax.swing.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE ).addComponent( this.nameTxt, javax.swing.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE ).addComponent( this.imagePathTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE ) ).addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED ).addComponent( this.selectImageBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE ).addGap( 38, 38, 38 ) ) ).addContainerGap( ) ).addGroup( layout.createSequentialGroup( ).addContainerGap( ).addComponent( this.errLbl ).addContainerGap( 433, Short.MAX_VALUE ) ) ) ;
        layout.setVerticalGroup( layout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING ).addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup( ).addComponent( this.header2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE ).addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED ).addComponent( this.errLbl ).addGap( 6, 6, 6 ).addGroup( layout.createParallelGroup( javax.swing.GroupLayout.Alignment.BASELINE ).addComponent( this.nameTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE ).addComponent( this.jLabel1 ) ).addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED ).addGroup( layout.createParallelGroup( javax.swing.GroupLayout.Alignment.BASELINE ).addComponent( this.jLabel2 ).addComponent( this.tooltipTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE ) ).addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED ).addGroup( layout.createParallelGroup( javax.swing.GroupLayout.Alignment.BASELINE ).addComponent( this.imagePathTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE ).addComponent( this.jLabel6 ).addComponent( this.selectImageBtn ) ).addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED ).addGroup( layout.createParallelGroup( javax.swing.GroupLayout.Alignment.BASELINE ).addComponent( this.jLabel3 ).addComponent( this.prefixTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE ) ).addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED ).addGroup( layout.createParallelGroup( javax.swing.GroupLayout.Alignment.BASELINE ).addComponent( this.jLabel4 ).addComponent( this.suffixTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE ) ).addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.UNRELATED ).addGroup( layout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING ).addGroup( layout.createSequentialGroup( ).addComponent( this.jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE ).addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.UNRELATED ).addGroup( layout.createParallelGroup( javax.swing.GroupLayout.Alignment.BASELINE ).addComponent( this.addButton ).addComponent( this.BtnCancel ) ) ).addComponent( this.jLabel5 ) ).addContainerGap( ) ) ) ;
    }// </editor-fold>//GEN-END:initComponents

    private void BtnCancelActionPerformed( java.awt.event.ActionEvent evt )
    {//GEN-FIRST:event_BtnCancelActionPerformed
        // TODO add your handling code here:
        this.dialog.setVisible( false ) ;
    }//GEN-LAST:event_BtnCancelActionPerformed

    private void selectImageBtnActionPerformed( java.awt.event.ActionEvent evt )
    {//GEN-FIRST:event_selectImageBtnActionPerformed
        IconImageChooser iconChooser = new IconImageChooser( ) ;
        IconImageChooser.createAndShowGUI( ) ;
        iconChooser.actionPerformed( evt ) ;
        this.buttonIconPath = iconChooser.getImagePath( ) ;
        this.imagePathTxt.setText( this.buttonIconPath ) ;
    }//GEN-LAST:event_selectImageBtnActionPerformed

    private void addButtonActionPerformed( java.awt.event.ActionEvent evt )
    {//GEN-FIRST:event_addButtonActionPerformed
        //validate form fields
        if( this.isDataValid( ) )
        {
            //set button properties
            this.buttonName = this.nameTxt.getText( ) ;
            this.buttonTooltip = this.tooltipTxt.getText( ) ;
            this.buttonPrefixCommand = this.prefixTxt.getText( ) ;
            this.buttonSuffixCommand = this.suffixTxt.getText( ) ;
            this.buttonExampleCommand = this.exampleCommandTxt.getText( ) ;
            ButtonEditor.latexButton = new LatexButton( this.buttonName, this.buttonIconPath, this.buttonTooltip, this.buttonPrefixCommand, this.buttonSuffixCommand, this.buttonExampleCommand ,"") ;
            this.BtnCancelActionPerformed( evt ) ;
        }
        else
        {
            this.errLbl.setText( this.invalidDataInfoText ) ;
        }
    }//GEN-LAST:event_addButtonActionPerformed

    private Boolean isDataValid( )
    {
        if( ( this.nameTxt.getText( ) == null ) || this.nameTxt.getText( ).isEmpty( ) )
        {
            this.invalidDataInfoText = "Action Button name is empty." ;
            return false ;
        }

        if( ( ( this.prefixTxt.getText( ) == null ) || this.prefixTxt.getText( ).isEmpty( ) )
            && ( ( this.suffixTxt.getText( ) == null ) || this.suffixTxt.getText( ).isEmpty( ) )
            && ( ( this.exampleCommandTxt.getText( ) == null ) || this.exampleCommandTxt.getText( ).isEmpty( ) ) )
        {
            this.invalidDataInfoText = "Prefix & Suffix or Example Command should be provided." ;
            return false ;
        }

        if( ( this.buttonIconPath == null ) || this.buttonIconPath.isEmpty( ) )
        {
            this.invalidDataInfoText = "You should select an icon for the button." ;
            return false ;
        }
        this.invalidDataInfoText = "" ;
        return true ;
    }

    public void setFields( LatexButton btn )
    {
        this.nameTxt.setText( btn.getName( ) ) ;
        this.tooltipTxt.setText( btn.getToolTipText( ) ) ;
        this.imagePathTxt.setText( btn.getIconPath( ) ) ;
        this.buttonIconPath = btn.getIconPath( ) ;
        this.prefixTxt.setText( btn.getPrefix( ) ) ;
        this.suffixTxt.setText( btn.getSuffix( ) ) ;
        this.exampleCommandTxt.setText( btn.getExpressionTemplate( ) ) ;
    }

    public static LatexButton getLatexButton( )
    {
        return ButtonEditor.latexButton ;
    }

    private static LatexButton latexButton = null ;

    private String buttonIconPath ;

    private String buttonName ;

    private String buttonTooltip ;

    private String buttonPrefixCommand ;

    private String buttonSuffixCommand ;

    private String buttonExampleCommand ;

    private String invalidDataInfoText = "" ;

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BtnCancel ;

    private javax.swing.JButton addButton ;

    private javax.swing.JLabel errLbl ;

    private javax.swing.JTextArea exampleCommandTxt ;

    private javax.swing.JPanel header2 ;

    private javax.swing.JTextField imagePathTxt ;

    private javax.swing.JLabel jLabel1 ;

    private javax.swing.JLabel jLabel2 ;

    private javax.swing.JLabel jLabel3 ;

    private javax.swing.JLabel jLabel4 ;

    private javax.swing.JLabel jLabel5 ;

    private javax.swing.JLabel jLabel6 ;

    private javax.swing.JScrollPane jScrollPane1 ;

    private javax.swing.JLabel label3 ;

    private javax.swing.JLabel label4 ;

    private javax.swing.JTextField nameTxt ;

    private javax.swing.JTextField prefixTxt ;

    private javax.swing.JButton selectImageBtn ;

    private javax.swing.JTextField suffixTxt ;

    private javax.swing.JTextField tooltipTxt ;
    // End of variables declaration//GEN-END:variables

}
